<template>
    <div class="box">            
        <!-- 左侧导航栏组件 -->      
       <div class="left">
            <van-sidebar v-model="activeKey" @change="onChange">
                <van-sidebar-item v-for="item in items" :title="item.name+''"  :key="item.id" />
            </van-sidebar>
       </div>
        <div class="right"> 
            <!-- 右侧下拉按钮组件 -->
            <template v-if="list.length" >
                <van-dropdown-menu active-color="#3cb963" :style="list.length>=3?'display:block;':'display:none'">
                    <van-dropdown-item  v-model="active" :options="option"  @change="itemChange"/>
                </van-dropdown-menu>
            </template>
            <van-tabs v-model="active">
                <van-tab :title="item.name"  v-for="item in list" :key="item.id" class="items">
                   <div>
                        <h2 ref="scrollTo">{{item.name}}</h2>
                        <ul>
                            <li v-for="itemx in item.products" :key="itemx.id"  @click="GoDetailPage(itemx)">
                                <div class="image">
                                    <img v-bind:src="itemx.small_image" alt="">
                                </div>
                                <div class="text">
                                    <h3>{{itemx.name}}</h3>
                                    <p class="depict">{{itemx.spec}}</p>
                                    <p class="price">
                                        <span class="dayPrice">￥{{itemx.price}}</span>
                                        <span class="originalPrice">￥{{itemx.origin_price}}</span>
                                    </p>    
                                </div>
                                <img src="../assets/car.png" class="shopping" alt=""  @click.stop="shopping(itemx)">
                            </li>
                        </ul>
                   </div>
                </van-tab>
            </van-tabs>
        </div>
    </div>
</template>

<script>
import $ from "@/utils/request"
// import CateList from "./CateList.vue";
export default {
    name: "LeftView",
    props: ["category"],
    data() {
        return {
            activeKey: 0,
            items: [],
            str: "homeApi/categoriesdetail/",
            list: [],
            active: 2,
            option:[],
        };
    },
    methods: {
        async onChange(index) {
            const sot = await $.getAxios(this.str + this.category[index]);
            // console.log(sot)
            this.list = sot.data.data.cate;
            this.option=sot.data.data.cate.map((item,index)=>{
                return {
                    text:item.name,
                    value:index
                }
            })
            console.log(this.list);
            //把分类页侧边栏的下边存入store
            this.$store.dispatch('setSidebarItemIndex',index) 
        },
        //关联上方滚动选项
        itemChange(index){
            this.active = index
        },
        GoDetailPage(item){
            this.$store.commit('setDetailPageData',item)
            sessionStorage.setItem("setDetailPageData",JSON.stringify(item))
            this.$router.push('/DetailPage').catch(err=>{})
        },
        shopping(item){
            this.$store.commit('setShoppingData',item)
            this.$toast('成功加入购物车');
        }
    },
    async created() {
        // 侧边栏数据
        let res = await this.$http.getCategoryApi();
        this.items = res.data.data.cate;
        // 右侧数据
        const sot = await $.getAxios("homeApi/categoriesdetail/lk001.json");
        // console.log(sot)
        this.list = sot.data.data.cate;
        console.log(this.list);

        this.activeKey = this.$store.getters.getCategoryIndex
    }
}
</script>

<style lang="scss" scoped>
::v-deep .van-tabs__line{
    display: none;
}  

::v-deep .van-dropdown-menu{
    position: fixed;
    width: 10%;
    height: 2.5rem;
    line-height: 2.5rem;
    right: 0;
    z-index: 1999;
    background-color: white;
}
::v-deep .van-dropdown-item--down {
    width:282px;
    margin-left:80px;
}
::v-deep .van-cell{
    background-color: #fff;
    display: block;
    text-align: center;
    margin-right: 2rem;
    margin-top: 0.4rem;
    width: 3.2rem;
    line-height: 1.4rem;
    height: 1.4rem;
    font-size: 0.65rem;
    border-radius: 1rem;
    color: rgba(0, 0, 0, 0.7);
    border: 1px solid grey;
    padding:0;
    .van-cell::after{
        display: none;
    }
}
::v-deep .van-popup--top{
    display: flex;
    flex-wrap: wrap;
    box-sizing: border-box;
    padding: .6rem 1rem;
}

::v-deep .van-dropdown-item__option--active {
   background-color: rgba(60, 185, 99, 0.1);
    color: #3cb963;
    border: 1px solid #3cb963;
}
::v-deep .van-ellipsis{
    display: none;   
}
.box{
    width:100vw;
    // height:35.65rem;
    .left{
        // width:100vw;
        height:35.65rem;
        // 强制去除滚动条
        overflow-y: scroll;
        -ms-overflow-style: none;
        overflow : -moz-scrollbars-none;
        &::-webkit-scrollbar {
            width:0 !important;
        }
    }
    .right{
        position: relative;
        width: calc(100vw - 5rem);
        // box-sizing: border-box;
        // padding-right: 2rem;
        height:35.65rem;
        // 强制去除滚动条
        overflow-y: scroll;
        -ms-overflow-style: none;
        overflow : -moz-scrollbars-none;
        &::-webkit-scrollbar {
            width:0 !important;
        }
        .van-tabs{
            width: calc(100vw - 5rem);
        }
        .items{
            width:100%;
            h2{
                display: inline-block;
                padding: 1rem;
                border-left: 3px solid #d9dde1;
                height: 0.32rem;
                line-height: .8rem;
                width: 92%;
                font-size: 0.73rem;
                color: #93999f;
                background-color: #f3f5f7;
                padding-left: 0.28rem;
                vertical-align: baseline;
            }
            ul{
                width:100%;
                padding:0;
                li{
                    padding: 0.36rem 0.36rem 0 0.36rem;
                    height:4.5rem;
                    display: flex;
                    z-index: 10;
                    justify-content: flex-start;
                    position: relative;
                    .shopping{
                        width: 1.8rem;
                        position: absolute;
                        top: 2.45rem;
                        right: 1.6rem;
                        z-index: 2000;
                    }
                    .image{
                        width: 4.0625rem;
                        height: 4.0625rem;
                        img{
                            width: 100%;
                            height: 100%;
                            border-radius: 0.04rem;
                            display: block;
                            background-size: contain;
                        }
                    }
                    .text{ 
                        h3{
                            font-size: 0.8375rem;
                            line-height: 1rem;
                            color: #07111b;
                            margin-bottom: 0.16rem;
                        }
                        .depict{
                            padding: 0.2rem;
                            line-height: 0.8rem;
                            font-size: 0.65rem;
                            color: #93999f;
                            margin-bottom: 0.16rem;
                        }
                        .price{
                            .dayPrice{
                                font-size: 0.75rem;
                                color: red;
                            }
                            .originalPrice{
                                padding-left: 0.5rem;
                                text-decoration: line-through;
                                font-size: 0.6875rem;
                                color: #b2b2b2;
                            }
                        }
                    }
                    border-bottom: 1px solid #eee;
                }
            }
        }
    }
    display: flex;
    // justify-content: space-between;
}
::v-deep .van-tabs__wrap{
    position: sticky;
    background: #fff;
    top: 0;
    left: 0;
    z-index: 22;
    height: 3rem;
    
}
</style>